package Maze

import (
	"fmt"
	"strconv"
)

var row int = 10
var col int = 10
var start []int = [] int{1, 0}
var end []int = [] int{row - 1, col - 1}
var maze [] [] int = [][]int{}

func init() {
	fmt.Println("init maze ....")



	maze = [][]int{
		{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},//0
		{1, 0, 0, 1, 0, 0, 0, 1, 0, 1},//1
		{1, 0, 0, 1, 0, 0, 0, 1, 0, 1},//2
		{1, 0, 0, 0, 0, 1, 1, 0, 0, 1},//3
		{1, 0, 1, 1, 1, 0, 0, 0, 0, 1},//4
		{1, 0, 0, 0, 1, 0, 0, 0, 0, 1},//5
		{1, 0, 1, 0, 0, 0, 1, 0, 0, 1},//6
		{1, 0, 1, 1, 1, 0, 1, 1, 0, 1},//7
		{1, 1, 0, 0, 0, 0, 0, 0, 0, 1},//8
		{1, 1, 1, 1, 1, 1, 1, 1, 0, 0},//9
	}

	//初始化
	/*for i := 0; i < row; i++ {

		for j := 0; j < col; j++ {

			if j == 0 {
				temp := make([]int, col)
				maze = append(maze, temp)
			}

			if i == 0 || i == row-1 || j == 0 || j == col-1 {

				maze[i][j] = 1

			} else {


				rand.Perm(0)

				n :=  rand.Intn(2)

				maze[i][j] = n

				fmt.Println(n)
			}

			if (i == start[0] && j == start[1]) || (i == end[0] && j == end[1]) {
				maze[i][j] = 0
			}

		}
	}*/
}

func main() {
	Passing := [][]int{start}
	Passingd := make(map[string]string)
	//开始走迷宫
	go_maze(start,Passing,Passingd)

	for k, v := range maze {

		fmt.Println(k, v)
	}

}

func go_maze(start [] int ,Passing [][]int,Passingd map[string]string) {

	current := start




	for true {

		x, y := current[0], current[1]

		if x-1 > 0 && Passingd[strconv.Itoa(x-1)+strconv.Itoa(y)] != "1" && maze[x-1][y] != 1 {
			Passing = append(Passing, current)
			Passingd[strconv.Itoa(x-1)+strconv.Itoa(y)] = "1"
			current = []int{x - 1, y}
			go_maze(current,Passing,Passingd)
		} else if y-1 > 0 && Passingd[strconv.Itoa(x)+strconv.Itoa(y-1)] != "1" && maze[x][y-1] != 1 {
			Passing = append(Passing, current)
			Passingd[strconv.Itoa(x)+strconv.Itoa(y-1)] = "1"
			current = []int{x, y - 1}
			go_maze(current,Passing,Passingd)
		} else if x+1 < row-1 && Passingd[strconv.Itoa(x+1)+strconv.Itoa(y)] != "1" && maze[x+1][y] != 1 {
			Passing = append(Passing, current)
			Passingd[strconv.Itoa(x+1)+strconv.Itoa(y)] = "1"
			current = []int{x + 1, y}
			go_maze(current,Passing,Passingd)
		} else if y+1 < col-1 && Passingd[strconv.Itoa(x)+strconv.Itoa(y+1)] != "1" && maze[x][y+1] != 1 {
			Passing = append(Passing, current)
			Passingd[strconv.Itoa(x)+strconv.Itoa(y+1)] = "1"
			current = []int{x, y + 1}
			go_maze(current,Passing,Passingd)
		} else if x == end[0] && y == end[1] {
			fmt.Println("走完啦!", Passing)
			panic("走完啦!!!")
		} else {
			if len(Passing) == 0 {
				fmt.Println("没有路走了.....")
				return
			} else {
				current = Passing[len(Passing)-1]
				//go_maze(current,Passing,Passingd)
				return
			}
		}
	}

}
